﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FrmPressDuctTerminal : System.Windows.Forms.Form
	{
		public FrmPressDuctTerminal()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.userSetting = new UserSetting();
			this.readDefault();
		}

		private void Init()
		{
			this.readDefault();
		}

		private void hyTerminalWidth_TextChanged(object sender, EventArgs e)
		{
			this.hytbThroatWidth.Text = this.hyTerminalWidth.Text;
		}

		private void hyTerminalHeight_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.hyTerminalHeight.Text))
			{
				this.hytbThroatHeight.Text = this.hyTerminalHeight.Text;
				return;
			}
			this.hytbThroatHeight.Text = (Convert.ToDouble(this.hyTerminalHeight.Text) - 250.0).ToString();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (!this.Validate(new List<YJKTextBox>
			{
				this.hyTerminalWidth,
				this.hyTerminalHeight,
				this.hyTerminalThickness,
				this.hytbThroatWidth,
				this.hytbThroatHeight,
				this.hyTextBox1,
				this.hyTextBox6,
				this.hytbOneFlow
			}))
			{
				return;
			}
			if (this.ConvertToDouble(this.hyTerminalWidth.Text) >= this.ConvertToDouble(this.hytbThroatWidth.Text) && this.ConvertToDouble(this.hyTerminalHeight.Text) >= this.ConvertToDouble(this.hytbThroatHeight.Text))
			{
				base.DialogResult = DialogResult.OK;
				this.writeDefault();
				base.Close();
				return;
			}
			YJKMessageBox.Information("风口高度风口宽度必须大于喉口高度喉口宽度！");
		}

		private void hytbOneFlow_TextChanged(object sender, EventArgs e)
		{
			double num = 0.0;
			double num2 = 0.0;
			if (this.hytbOneFlow.Text.Trim() != "")
			{
				if (Convert.ToDouble(string.IsNullOrEmpty(this.hytbThroatWidth.Text) ? "0" : this.hytbThroatWidth.Text) > 0.0001 && Convert.ToDouble(string.IsNullOrEmpty(this.hytbThroatHeight.Text) ? "0" : this.hytbThroatHeight.Text) > 0.0001 && Convert.ToDouble(string.IsNullOrEmpty(this.hyTextBox1.Text) ? "0" : this.hyTextBox1.Text) > 0.0001)
				{
					num2 = Convert.ToDouble(this.hytbThroatWidth.Text) * Convert.ToDouble(this.hytbThroatHeight.Text) * Convert.ToDouble(this.hyTextBox1.Text) / 1000000.0;
				}
				try
				{
					num = double.Parse(this.hytbOneFlow.Text.Trim());
				}
				catch (Exception)
				{
				}
				if (num2 > 0.0)
				{
					this.hytbSpeed.Text = StringTools.CutOffEfficacious((num / num2 / 3600.0).ToString(), 2);
				}
			}
		}

		private bool Validate(List<YJKTextBox> controls)
		{
			foreach (YJKTextBox hytextBox in controls)
			{
				if (string.IsNullOrEmpty(hytextBox.Text))
				{
					YJKMessageBox.Information(hytextBox.Tag + "不能为空！");
					hytextBox.Focus();
					return false;
				}
				if (hytextBox.Name.ToString() == "hyTextBox6")
				{
					return true;
				}
				if (hytextBox.Name.ToString() == "hyTextBox1" && Convert.ToDouble(hytextBox.Text) > 1.0)
				{
					YJKMessageBox.Information(hytextBox.Tag + "不可大于1！");
					hytextBox.Focus();
					return false;
				}
				if (Convert.ToDouble(hytextBox.Text) < 0.0001)
				{
					YJKMessageBox.Information(hytextBox.Tag + "需大于0！");
					hytextBox.Focus();
					return false;
				}
			}
			return true;
		}

		private double ConvertToDouble(string text)
		{
			if (!string.IsNullOrEmpty(text))
			{
				return Convert.ToDouble(text);
			}
			return 0.0;
		}

		private void readDefault()
		{
			this.hyTerminalWidth.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "TerminalWidth")) ? "400" : this.userSetting.GetValue("PressDuctTerminal", "TerminalWidth"));
			this.hyTerminalHeight.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "TerminalHeight")) ? "1250" : this.userSetting.GetValue("PressDuctTerminal", "TerminalHeight"));
			this.hyTerminalThickness.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "TerminalThickness")) ? "50" : this.userSetting.GetValue("PressDuctTerminal", "TerminalThickness"));
			this.hytbThroatWidth.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "ThroatWidth")) ? "400" : this.userSetting.GetValue("PressDuctTerminal", "ThroatWidth"));
			this.hytbThroatHeight.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "ThroatHeight")) ? "1000" : this.userSetting.GetValue("PressDuctTerminal", "ThroatHeight"));
			this.hyTextBox6.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "HighLevel")) ? "0.3" : this.userSetting.GetValue("PressDuctTerminal", "HighLevel"));
			this.hyTextBox1.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "Area")) ? "0.9" : this.userSetting.GetValue("PressDuctTerminal", "Area"));
			this.hytbOneFlow.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("PressDuctTerminal", "OneFlow")) ? "8000" : this.userSetting.GetValue("PressDuctTerminal", "OneFlow"));
		}

		private void writeDefault()
		{
			this.userSetting.WriteValue("PressDuctTerminal", "TerminalWidth", this.hyTerminalWidth.Text);
			this.userSetting.WriteValue("PressDuctTerminal", "TerminalHeight", this.hyTerminalHeight.Text);
			this.userSetting.WriteValue("PressDuctTerminal", "TerminalThickness", this.hyTerminalThickness.Text);
			this.userSetting.WriteValue("PressDuctTerminal", "ThroatWidth", this.hytbThroatWidth.Text);
			this.userSetting.WriteValue("PressDuctTerminal", "ThroatHeight", this.hytbThroatHeight.Text);
			this.userSetting.WriteValue("PressDuctTerminal", "HighLevel", this.hyTextBox6.Text);
			this.userSetting.WriteValue("PressDuctTerminal", "Area", this.hyTextBox1.Text);
			this.userSetting.WriteValue("PressDuctTerminal", "OneFlow", this.hytbOneFlow.Text);
		}

		private UserSetting userSetting;
	}
}
